//
//  FMSettingViewController.m
//  PsychologyFM
//
//  Created by zhang on 16/1/11.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMSettingViewController.h"
#import "FMLoginViewController.h"
#import "UMSocial.h"
#import "FMSettingLoginedView.h"
#import "FMSettingUnLoginView.h"
#import <UIButton+WebCache.h>

#import "FMSettingLoginedView.h"
#import "FMSettingUnLoginView.h"
#import <UIButton+WebCache.h>
#import <FMDB.h>
#import "FMDataBaseModel.h"


@interface FMSettingViewController ()<UMSocialUIDelegate>

@property (nonatomic,strong) FMDatabase *db;
@property (nonatomic,strong) NSMutableArray *dataBaseArray;

@property (nonatomic,strong) FMDataBaseModel *databaseModel;

@end

@implementation FMSettingViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"设置";
}



- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //  根据数据库查找
    //  创建我们的数据库的路径 同时拼接数据库的文件名称
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"FMDataBase.db"];
    //  根据路径去创建一个数据库
    _db = [FMDatabase databaseWithPath:path];
    
//    NSLog(@"%@",path);
    
    //  调用open方法 打开成功返回YES 失败 返回NO
    if ([_db open]) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
    //  分配空间
    _dataBaseArray = [NSMutableArray array];
    
    //  查找取值
    //  执行在查找的操作 调用的方法是 executeQuery 参数是SQL的查找语句
    //  会返回一个 FMResultSet 类型的值
    FMResultSet *result = [_db executeQuery:@"SELECT * FROM UserInformationlist"];
    while ([result next]) {
        //(isLogin text,avatar text, user_id text, introduce text, nickname text, qq_openid text, renren_openid text, sina_openid text, expire text, token text)
        NSString *isLogin = [result stringForColumn:@"isLogin"];
        NSString *avatar = [result stringForColumn:@"avatar"];
        NSString *user_id = [result stringForColumn:@"user_id"];
        NSString *introduce = [result stringForColumn:@"introduce"];
        NSString *nickname = [result stringForColumn:@"nickname"];
        NSString *qq_openid = [result stringForColumn:@"qq_openid"];
        NSString *renren_openid = [result stringForColumn:@"renren_openid"];
        NSString *sina_openid = [result stringForColumn:@"sina_openid"];
        NSString *expire = [result stringForColumn:@"expire"];
        NSString *token = [result stringForColumn:@"token"];
        
        NSLog(@"%@",isLogin);
        //  一定要分配空间
        _databaseModel = [[FMDataBaseModel alloc]init];
        _databaseModel.isLogin = isLogin;
        _databaseModel.avatar = avatar;
        _databaseModel.user_id = user_id;
        _databaseModel.nickname = nickname;
        _databaseModel.qq_openid = qq_openid;
        _databaseModel.renren_openid = renren_openid;
        _databaseModel.sina_openid = sina_openid;
        _databaseModel.introduce = introduce;
        _databaseModel.expire = expire;
        _databaseModel.token = token;
        
        [_dataBaseArray addObject:_databaseModel];
    }
    if (_dataBaseArray.count == 0) {
        FMSettingUnLoginView *view= [[FMSettingUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        //  为按钮添加方法
        [view.iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.loginButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.alarmButton addTarget:self action:@selector(alarmButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        
        [self.view addSubview:view];
    }else
    {
    FMDataBaseModel *model = _dataBaseArray[0];
    //FMDataBaseModel *model = _databaseModel;
    NSString *isLogin = model.isLogin;
    if ([isLogin isEqualToString:@"YES"]) {
        FMSettingLoginedView *view = [[FMSettingLoginedView alloc]initWithFrame:self.view.bounds];
        //  给对应的空间赋值
        [view.iconButton sd_setImageWithURL:[NSURL URLWithString:model.avatar] forState:(UIControlStateNormal) completed:nil];
        NSLog(@"%@",model.nickname);
        [view.loginButton setTitle:model.nickname forState:(UIControlStateNormal)];
        //view.loginButton.titleLabel.text = model.nickname;
        view.changeInformationButton.titleLabel.text = @"修改资料";
        
        //  为退出按钮添加方法
        [view.quitButton addTarget:self action:@selector(quitButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.alarmButton addTarget:self action:@selector(alarmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [view.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self.view addSubview:view];
    }else
    {
        FMSettingUnLoginView *view= [[FMSettingUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        //  为按钮添加方法
        [view.iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.loginButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [view.alarmButton addTarget:self action:@selector(alarmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.view addSubview:view];
        
    }
    
    }
    //  注册通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notification:) name:@"userHadLogined" object:nil];
    
    [center addObserver:self selector:@selector(userQuitNotification:) name:@"userQuit" object:nil];

}

//  未登录点击头像和昵称都是触发该方法
- (void)iconButtonAction:(UIButton *)sender
{
    //得判断 如果没有登录 跳到登录界面
    //  登陆过了
    NSLog(@"跳到登录界面");
    FMLoginViewController *loginVC = [[FMLoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)alarmButtonAction:(UIButton *)sender
{
    NSLog(@"闹钟设置按钮");
//    UIAlertView *alertVC = [UIAlertView alertViewWithTitle:@"提示" message:@"删除成功" preferredStyle:(UIAlertControllerStyleAlert)];
//    [alertVC.view show];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"闹钟设置按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
    [alertView show];
}


//  定时开关点击方法
- (void)timedSwitchAction:(UISwitch *)sender
{
    
}
//  qq开关点击方法
- (void)qqSwitchAction:(UISwitch *)sender
{
    
}
//  sina开关点击方法
- (void)sinaSwitchAction:(UISwitch *)sender
{
    
}
//  清楚缓存按钮点击方法
- (void)clearCacheButtonAction:(UIButton *)sender
{
//    FMMapViewController *map = [[FMMapViewController alloc] init];
//    [self.navigationController pushViewController:map animated:YES];
}
//  分享按钮点击方法
- (void)shareButtonAction:(UIButton *)sender
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"569f91d767e58e4a490002c9"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,nil]
                                       delegate:self];
}
//  鼓励按钮点击方法
- (void)encourageButtonAction:(UIButton *)sender
{
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://mesu.apple.com/assets/com_apple_MobileAsset_SafariCloudHistoryConfiguration"]];
}
//  反馈按钮点击方法
- (void)feedBackButtonAction:(UIButton *)sender
{
    
}
//  退出按钮点击方法
- (void)quitButtonAction:(UIButton *)sender
{
    //  删除数据库
    //  删除操作,只保存当前登录用户的状态
    [_db executeUpdate:@"DELETE FROM UserInformationlist"];
    
    //  注册通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"userQuit" object:self userInfo:nil];
    
}

//  接收通知执行的方法
- (void)notification:(NSNotification *)notification
{
    FMSettingLoginedView *view= [[FMSettingLoginedView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    //  根据请求有成功的值为对应的空间赋值
    
    NSDictionary *dic = notification.userInfo;
//    NSLog(@"%@",dic);
    [view.iconButton sd_setImageWithURL:[NSURL URLWithString:dic[@"data"][@"avatar"]] forState:(UIControlStateNormal) placeholderImage:nil];
    [view.loginButton setTitle:dic[@"data"][@"nickname"] forState:(UIControlStateNormal)];

    
    
    //  为按钮添加方法
    //  分享按钮
    [view.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    //  退出按钮
    [view.quitButton addTarget:self action:@selector(quitButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.view = view;
    
}

- (void)userQuitNotification:(NSNotification *)notification
{
    FMSettingUnLoginView *view= [[FMSettingUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    //  为按钮添加方法
    [view.iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [view.loginButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [view.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.view = view;
}

@end
